Thread: const char[] or string?

  1. #1
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820

    const char[] or string?

    Hey guys i have a quick question. Im currently using a const char to print out a message that is printed to the screen alot eg.
    Code:
    const char welcomeMsg[6]"Hello";
    should i use the string class instead or is it a matter of preferance?
    Woop?

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Because the message is constant, ie, it's not supposed to change, an STL string would be a bit of overkill. When you need to parse, split, or grow a string, use STL.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Ok cool thanks Benny
    Woop?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Because the message is constant, ie, it's not supposed to change, an STL string would be a bit of overkill.
    Even if it's not supposed to change, the member functions and overloaded operators of the string class can be mighty useful. Consider the following method:
    Code:
    const char welcomeMsg[] = "Hello";
    Now, the advantage of strings is diminished because to compare with welcomeMsg (assuming the non-const strings are declared std::string X;) you still must include <cstring> and use strcpy and friends:
    Code:
    if (strcpy(X.c_str(), welcomeMsg) == 0) {
      // Equal
    }
    The same goes for every other operation with welcomeMsg that requires a C-style string function. On the other hand, if welcomeMsg were declared as such:
    Code:
    const std::string welcomeMsg("Hello");
    The complex comparison for equality would simply be:
    Code:
    if (X == welcomeMsg) {
      // Equal
    }
    And the same advantage is true for any other overloaded operators. Another advantage is that now you can work easily with welcomeMsg if the need arises without resorting to C-style string functions or hand-rolled functions if the C library doesn't support what you want in some fashion. The C++ string class has a respectable number of member functions that are useful even with const strings.

    Conclusion? Don't mix C and C++ strings when you can avoid it. This almost always results in not using C-style strings and a considerable improvement in the quality and robustness of the code.
    Last edited by Prelude; 07-10-2004 at 08:34 AM.
    My best code is written with the delete key.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    if (strcpy(X.c_str(), welcomeMsg) == 0) {
      // Equal
    }
    Don't you mean strcmp()?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    35
    Indeed, that code compares a char pointer with 0, which has to be considered an error.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Don't you mean strcmp()?
    Yes. My fingers were running on autopilot.
    My best code is written with the delete key.

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I still think it's overkill for a simple welcome message.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    ya i have no need to compare, add to or anything like that im just using it so i don't have to type so much
    Woop?

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I still think it's overkill for a simple welcome message.
    The C++ string class was designed to be comparable in size and speed with C-style strings. Also, because of the hidden implementation, library writers have quite a bit of freedom in making std::string superior to C-style strings in the way of performance and space costs. I think your hesitation is unfounded.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I think there are some advantages to localization if he stored the data in a central location such as a resource(etc.) There, I think he'd pretty much have to store c-style strings. Besides, even when writing code such as std::string("Hello") the "Hello" characters will be stored as static c-style array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 03:13 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM